* Creates Figure A8

clear all

use "$savedata/masterdata.dta", replace

keep if sample25==1
gen vol = vol25

* Merge in trust characteristics
gen trust = trust_code
merge m:1 trust using "$inputs/types.dta"
drop if _merge==2
rename _merge _mergetrust
merge m:1 trust using "$staffdata/cardiologists_201718.dta"
drop if _merge==2
rename _merge _mergetrust2


* Create quality metric

reghdfe survive30 c.prevyear_cost, absorb(i.sex##i.derv_age i.black i.mixed i.chinese i.asian i.race_miss i.ynch* i.prevyear_stroke i.di1 i.di2 i.di3 i.di4 i.di5 i.shock i.arythmia i.arthero i.arrest i.dow##i.admidate_mont##i.finyear i.hyid, savefe) keepsingleton
predict residual, residuals

xtreg residual, fe i(doctor_id)
predict docfe30, u
gen sigma_u = e(sigma_u)
gen sigma_e = e(sigma_e)

gen signal_2step = ((sigma_u^2) / ((sigma_u^2) + ((sigma_e^2)/vol)))
gen adj_docfe30 = docfe*signal_2step

* Create up-to-date teaching variable
gen teach = 0
replace teach = 1 if trusttype==6
drop teaching

keep if finyear==2017 

collapse (mean) *docfe* has_cea count fte_contracted fte_actual british british_trained age female teach trusttype tenure_orig part_time_contracted (sum) one, by(trust_code)

* Output Figure A8
twoway kdensity adj_docfe if teach==1, lcolor(black) || kdensity adj_docfe if teach==0, lcolor(red) lpattern(dash) xtitle(Estimated doctor FE (30-days)) graphregion(color(white)) legend(label(1 "Teaching Hospitals") label (2 "Non-Teaching Hospitals")) xlab(-0.01(0.005)0.01) xscale(range(-0.01(0.005)0.01)) ytitle("Density")
graph export "$results/FigureA8.pdf", as(pdf) replace

